package com.wuyancheng.controller;

import com.wuyancheng.pojo.Resume;
import com.wuyancheng.service.IResumeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/resume")
public class ResumeController {

    @Autowired
    private IResumeService resumeService;

    @RequestMapping(value = "/query")
    public ModelAndView findAllResume(){
        ModelAndView modelAndView = new ModelAndView();
        List<Resume> allResume = resumeService.findAllResume();
        modelAndView.addObject("allResume", allResume);
        modelAndView.setViewName("success");
        return modelAndView;
    }

    @RequestMapping(value = "/saveEdit")
    public String saveEdit(){
        return "saveEdit";
    }

    @RequestMapping(value = "/save",method = {RequestMethod.POST})
    public ModelAndView save(Resume resume){
        resumeService.saveResume(resume);
        ModelAndView modelAndView = new ModelAndView();
        List<Resume> allResume = resumeService.findAllResume();
        modelAndView.addObject("allResume", allResume);
        modelAndView.setViewName("success");
        return modelAndView;
    }

    @RequestMapping(value = "/deleteEdit")
    public ModelAndView deleteEdit(long id){
        Resume oneResume = resumeService.findOneResume(id);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("oneResume", oneResume);
        modelAndView.setViewName("deleteEdit");
        return modelAndView;
    }

    @RequestMapping(value = "/updateEdit")
    public ModelAndView updateEdit(long id){
        Resume oneResume = resumeService.findOneResume(id);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("oneResume", oneResume);
        modelAndView.setViewName("updateEdit");
        return modelAndView;
    }

    @RequestMapping(value = "/delete", method = {RequestMethod.POST})
    public ModelAndView delte(Long id){
        resumeService.deleteResume(id);
        ModelAndView modelAndView = new ModelAndView();
        List<Resume> allResume = resumeService.findAllResume();
        modelAndView.addObject("allResume", allResume);
        modelAndView.setViewName("success");
        return modelAndView;
    }

}
